package com.ncloud.chnl.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.chnl.business.CustUserBusiness;
import com.ncloud.chnl.common.Constant;
import com.ncloud.chnl.entity.CustAreaUser;
import com.ncloud.chnl.mapper.CustAreaUserMapper;
import com.ncloud.chnl.service.CustAreaUserService;
import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.param.service.SequenceService;

@Service
public class CustAreaUserServiceImpl implements CustAreaUserService {
	
	@Reference
	private SequenceService sequence;
	@Autowired
	private CustAreaUserMapper mapper;
	@Autowired
	private CustUserBusiness business;
	
	@Override
	public Object createCustAreaUserRel(JSONObject json) {
		// 校验参数
		if(!json.containsKey("areaId") || !json.containsKey("userId") || !json.containsKey("custId")){
			throw new UserException(ResultEnum.PARAM_ERROR);
		}
		// 组织参数
		Long userId = json.getLong("userID");
		Long relInstId = sequence.getSequence("REL_INST_ID_SEQ");
		CustAreaUser areaUser = JSON.parseObject(json.toJSONString(), CustAreaUser.class);
		areaUser.setUserId(json.getLong("userId"));
		if(areaUser.getIsManager()==null) areaUser.setIsManager(Constant.CUST_AREA_USER_IS_UNMANAGER);
		areaUser.setCreateUser(userId);
		areaUser.setUpdateUser(userId);
		areaUser.setCreateTime(new Date());
		areaUser.setUpdateTime(new Date());
		areaUser.setRelInstId(relInstId);
		areaUser.setState(Constant.CUST_AREA_NODE_STATE_NORMAL);
		// 插入数据库
		try {
			mapper.insertCustAreaUser(areaUser);
		} catch (Exception e) {
			throw new UserException(ResultEnum.DATABASE_ERROR+e.toString());
		}
		return HttpStatus.CREATED;
	}

	@Override
	public Object deleteCustAreaUserRel(JSONObject json) {
		if(!json.containsKey("relInstId")){
			throw new UserException(ResultEnum.PARAM_ERROR);
		}
		Long relInstId = json.getLong("relInstId");
		Long userId = json.getLong("userID");
		CustAreaUser areaUser = new CustAreaUser();
		areaUser.setRelInstId(relInstId);
		areaUser.setState(Constant.CUST_AREA_NODE_STATE_DELETE);
		areaUser.setUpdateTime(new Date());
		areaUser.setUpdateUser(userId);
		try {
			mapper.updateCustAreaUser(areaUser);
		} catch (Exception e) {
			throw new UserException(ResultEnum.DATABASE_ERROR+e.toString());
		}
		return HttpStatus.OK;
	}

	@Override
	public JSONArray getCustAreaUserList(JSONObject json) {
		if(!json.containsKey("areaId")){
			throw new UserException(ResultEnum.PARAM_ERROR);
		}
		Long areaId = json.getLong("areaId");
		List<Map<String, Object>> areaUsers = mapper.getAllCustAreaUsers(areaId);
		return JSON.parseArray(JSON.toJSONString(areaUsers));
	}

	@Override
	public JSONObject getCustAreaUserRelByUserId(Long userId) {
		CustAreaUser custAreaUser = mapper.getCustAreaUserByUserId(userId);
		return custAreaUser==null?new JSONObject():JSON.parseObject(JSON.toJSONString(custAreaUser));
	}

	@Override
	public Object batchDealCustAreaUser(JSONObject json) {
		if((json.containsKey("add") && !json.getJSONArray("add").isEmpty())
				||(json.containsKey("del") && !json.getJSONArray("del").isEmpty())){
			business.batchDealCustAreaUser(json);
		}else{
			throw new UserException(ResultEnum.PARAM_ERROR);
		}
		return HttpStatus.OK;
	}

	@Override
	public void deleteCustAreaUserByAreaId(JSONObject json) {
		Long areaId = json.getLong("areaId");
		Long userId = json.getLong("userID");
		mapper.deleteCustAreaUsers(areaId,userId);		
	}

}
